<?php
Class C_surnames extends Cemvc_Control_Base
{
  public function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    $this->display();
  }
  
  public function build()
  {
    $user = new M_qin_users();
    $res = $user->get_surnames();
    $r = M_Tools::convert_mysql_results_to_array($res, "surname", "num");
    $s1 = M_Tools::get_all_surnames();
    $s2 = array();
    foreach ($s1 as $surname => $id)
      {
	$surname = trim($surname);
	$s2[$surname] = isset($r[$surname]) ? $r[$surname] : 0;
      }
    arsort($s2);
    $this->view->assign("surnames", $s2);
    $this->view->assign("surnames_ids", $s1);

    $this->display();
  }
  
  public function show()
  {
    if (!isset($_REQUEST["surname"]) || empty($_REQUEST["surname"]))
      die("You must specified a surname to search.");

    $s1 = M_Tools::get_all_surnames();
    $surname = array_search($_REQUEST["surname"], $s1);

    $user = new M_qin_users();
    $res = $user->get_users_with_surname($surname); 
    $this->assign("page", $user->showPages());

    $users = array();
    foreach($res as $u)
      {
	$u["figure"] = M_Tools::get_figure_url($u["figureurl"], $u["gender"]);
	$u["name"] = function_exists("mb_substr") ? mb_substr($u["realname"], 0, NAME_LENGTH, 'utf-8') : $u["realname"];
	$users[] = $u;
      }

    $this->view->assign("surname", $surname);
    $this->view->assign("users", $users);
    $this->display();
  }
};
